package com.mxprojects.gui
{
	import com.mxprojects.constructors.ButtonHighlight;
	import com.mxprojects.constructors.CheckboxConstructor;
	import com.mxprojects.io.BoardCookie;
	import com.mxprojects.io.Config;
	import com.mxprojects.io.CustomEvent;
	import com.mxprojects.language.Word;
	import com.mxprojects.views.PrivateMessages;
	
	import flash.events.MouseEvent;
	import flash.text.TextField;
	
	public class PrivateItem extends BaseForumElement
	{
		public static const NAME:String = "pm";
		
		private const DIVIDER_COUNT:int = 3;
		private const USER_TYPE_RECIPIENT:String = "recipient";
		private const USER_TYPE_SENDER:String = "sender";
		
		public var username:TextField;
		public var subject:TextField;
		public var date:TextField;
		
		public var checkBox:CheckboxConstructor;		
		
		//stuff to share
		private var mainButton:ButtonHighlight;
		private var messageID:int;
		private var recipientID:int;
		private var recipientStatus:int;
		private var senderID:int;
		private var senderStatus:int;
		private var displayName:String;
		private var pmSubject:String;
		private var pmMessage:String;
		private var pmDate:String;
		private var replied:String;		
		private var privateHeader:PrivateHeader;
		
		public function PrivateItem(messageID:int, recipientID:int, recipientStatus:int, senderID:int, senderStatus:int, displayName:String, subject:String, message:String, sent:String, replied:String, privateHeader:PrivateHeader){
			//set the CSS stuff
			username.styleSheet = Styles.css;
			this.subject.styleSheet = Styles.css;
			date.styleSheet = Styles.css;
			
			//get the data from the private header
			background.width = privateHeader.background.width;
			username.x = privateHeader.username.x;
			username.width = privateHeader.username.width;
			this.subject.x = privateHeader.subject.x;
			this.subject.width = privateHeader.subject.width;
			date.x = privateHeader.date.x;
			date.width = privateHeader.date.width;
			
			//setup the textfields
			username.htmlText = "<span class=\"privateUsername\">" + displayName + "</span>";
			this.subject.htmlText = "<span class=\"privateSubject\">" + subject + "</span>";
			date.htmlText = "<span class=\"privateDate\">" + sent + "</span>";
			
			//if the message is new bold it
			if(recipientStatus == PrivateMessages.NEW || senderStatus == PrivateMessages.NEW){
				username.htmlText = "<span class=\"privateUsernameNew\">" + displayName + "</span>";
				this.subject.htmlText = "<span class=\"privateSubjectNew\">" + subject + "</span>";
				date.htmlText = "<span class=\"privateDateNew\">" + sent + "</span>";
			}
			
			//share the love
			this.messageID = messageID;
			this.recipientID = recipientID;
			this.recipientStatus = recipientStatus;
			this.senderID = senderID;
			this.senderStatus = senderStatus;
			this.displayName = displayName;
			this.pmSubject = subject;
			this.pmMessage = message;
			this.pmDate = date.text;
			this.replied = replied;
			this.privateHeader = privateHeader;
			
			//add a button
			mainButton = new ButtonHighlight(this, viewMessage);
			addChild(mainButton);
			
			//build something to send with the checkboxes to let PHP know if the mesageID has the user as a sender or a recipient
			var userType:String = USER_TYPE_RECIPIENT;
			if(senderID == int(BoardCookie.getUserID())){
				userType = USER_TYPE_SENDER;
			}
			
			//place a checkbox to delete the message
			checkBox = new CheckboxConstructor("", String(messageID) + ":" + userType);
			checkBox.x = privateHeader.checkBox.x;
			checkBox.y = Math.round(background.height/2 - checkBox.height/2);
			addChild(checkBox);
			
			//if any checkboxs are fired in the items then the header checkbox needs to be set to off
			checkBox.addEventListener(CheckboxConstructor.TOGGLE, resetHeaderCheckBox, false, 0, true);
			
			//add a status icon
			var statusToShow:String;
			if(recipientStatus == PrivateMessages.NEW || senderStatus == PrivateMessages.NEW){
				statusToShow = StatusIcon.NEW;
			}else{
				statusToShow = StatusIcon.OLD;
			}
			
			var statusIcon:StatusIcon = new StatusIcon(statusToShow, Word.PRIVATE_MESSAGES);
			statusIcon.x = Math.round(checkBox.x + checkBox.background.width + Config.TEXT_PADDING/2);
			statusIcon.y = Math.round(background.height/2 - statusIcon.height/2);
			addChild(statusIcon);
			
			//see if it was replied to
			if(this.replied != "0"){
				var repliedIcon:StatusIcon = new StatusIcon(StatusIcon.REPLIED, Word.PRIVATE_MESSAGES);
				repliedIcon.x = statusIcon.x;
				repliedIcon.y = statusIcon.y;
				addChild(repliedIcon);
			}
			
			//parse the CSS
			super.init(".privateBackground", DIVIDER_COUNT, false);
			
			//place the stuff where it needs to be
			//placeStuff();
		}
		
		public function setCheckBox(isChecked:Boolean):void {
			//this just sets the state of the checkbox
			checkBox.setCheckState(isChecked);
		}
		
		override public function resize():void {
			placeStuff();
			super.resize();
		}
		
		private function resetHeaderCheckBox(event:CustomEvent):void {
			//this will automatically false the header checkbox anytime any item checkbox is clicked
			privateHeader.checkBox.setCheckState(false);
		}
		
		private function placeStuff():void {
			//stretch out the BG
			background.width = parent.width;
			
			//place titles in proper spot
			subject.x = username.x + username.width + Config.TEXT_PADDING;
			subject.width = Math.round(background.width - subject.x - date.width - Config.TEXT_PADDING*2);
			date.x = Math.round(subject.x + subject.width + Config.TEXT_PADDING); 
			
			//move the dividers
			getDivider(0).x = Math.round(username.x - (Config.TEXT_PADDING/2));
			getDivider(1).x = Math.round(subject.x - (Config.TEXT_PADDING/2));
			getDivider(2).x = Math.round(date.x - (Config.TEXT_PADDING/2));
			
			//buttons
			mainButton.width = background.width - getDivider(0).x - getDivider(0).width;
			mainButton.x = getDivider(0).x;
		}
		
		private function viewMessage(event:MouseEvent):void {			
			//view the message
			PrivateViewMessage.viewMessage(messageID, recipientID, recipientStatus, senderID, senderStatus, displayName, pmSubject, pmMessage, pmDate, replied);
		}
		
	}
}